package com.apifamily.Client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.apifamily.model.BaiduParams;
import com.apifamily.model.QQParams;
import com.apifamily.model.User;

import java.util.HashMap;
import java.util.Map;

import static com.apifamily.Utils.SignUtil.getSign;


public class ApiClient {
    public static final String GATEWAY_HOST = "http://localhost:8082";
    private String accessKey;
    private String secretKey;

    public ApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public String getNameGet(String name){
        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        String result= HttpUtil.get(GATEWAY_HOST + "/api/name/get", paramMap);
        System.out.println(result);
        return result;
    }

    public String getNamePost(String name){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);

        String result= HttpUtil.post(GATEWAY_HOST + "/api/name/post", paramMap);
        System.out.println(result);
        return result;
    }

    public String getUserNamePost(User user){
        String json = JSONUtil.toJsonStr(user);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/name/user")
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute();
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        System.out.println(result);
        return result;
    }

    private Map<String,String> getHeaderMap(String body){
        Map<String,String> hashMap = new HashMap<>();
        hashMap.put("accessKey",accessKey);//标识
        //一定不能直接发送
        /*hashMap.put("secretKey",secretKey);*/
        hashMap.put("nonce", RandomUtil.randomNumbers(4));//once随机数
        hashMap.put("body",body);//用户参数
        hashMap.put("timestamp",String.valueOf(System.currentTimeMillis() / 1000));//时间戳
        hashMap.put("sign", getSign(body,secretKey));//签名
        return hashMap;
    }



    public String getUsernameByPost(User user){

        String json = JSONUtil.toJsonStr(user);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/name/user")
                .addHeaders(getHeaderMap(json))
                .body(json)
                .execute();
        System.out.println(httpResponse.getStatus());
        String body = httpResponse.body();
        System.out.println(body);
        return body;
    }

    public String getAvatarUrlByPost(){
        HttpResponse httpResponse = HttpRequest.get(GATEWAY_HOST + "/api/avatar/avatarUrl")
                .addHeaders(getHeaderMap(""))
                .execute();
        System.out.println(httpResponse.getStatus());
        String body = httpResponse.body();
        System.out.println(body);
        return body;
    }



    public String onlineInvoke(String parameters,String url) {
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + url)
                .addHeaders(getHeaderMap(parameters))
                .body(parameters)
                .execute();
        System.out.println(httpResponse.getStatus());
        String result = httpResponse.body();
        return result;
    }

    public String getBaiduInfo(BaiduParams baiduParams){
        String parameters = JSONUtil.toJsonStr(baiduParams);
        String result = onlineInvoke(parameters, "/api/baidu/baiduInfo");
        return result;
    }

}
